package com.example.newsdemo;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class ViewMoreCommentsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<Comment> commentsdata = new ArrayList<>();
    private CommentsSectionAdapter adapter;
    private Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_more_comments);


//        initHandler();
        initFindBmobDataForView();
        initRecyclerView();

        
    }

//    private void initHandler() {
//        handler = new Handler(new Handler.Callback() {
//            @Override
//            public boolean handleMessage(Message msg) {
//                adapter.changData(commentsdata);
//                return false;
//            }
//        });
//    }

    private void initFindBmobDataForView() {
        Intent intent = getIntent();
        String newsID =intent.getStringExtra("NEWSID");
        Log.e("more","newsID="+newsID);
        BmobQuery<Comment> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("news_id",newsID);
        bmobQuery.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> object, BmobException e) {
                if (e == null) {
                    for (Comment comment : object) {
                        comment.getUser_id();
                        comment.getContent();
                        commentsdata.add(comment);
                        adapter.changData(commentsdata);

                    }
                }else{
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }


    private void initRecyclerView() {
        recyclerView = findViewById(R.id.list_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ViewMoreCommentsActivity.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new CommentsSectionAdapter(ViewMoreCommentsActivity.this,commentsdata);
        recyclerView.setAdapter(adapter);

    }
}
